﻿namespace EzDesk.DesktopClient.ViewModel
{
    using EzDesk.DesktopClient.ViewContract;
    using EzDesk.MVVMBase;
    using EzDesk.DesktopClient.Event;
    using EzDesk.Client.Utility;
    using System.Collections.Generic;
    using EzDesk.EventAggregation;
    using EzDesk.Generic;
    using System.Collections.ObjectModel;
    using System;
    using EzDesk.DesktopClient.View;
    using EzDesk.DesktopClient.Model;
    using EzDesk.Generic.Collections;
    using System.Linq;
    using EzDesk.DesktopClient.MVVMEnhance;

    public class FeedViewModel : ViewModelWithSingleContainer<IFeedView>
    {
        public RangeCollection<FeedItemViewModel> NotificationFeeds
        {
            get;
            set;
        }

        public RangeCollection<FeedItemViewModel> UpdateFeeds
        {
            get;
            set;
        }
        public FeedViewModel()
            : base()
        {
            InitMember();
            InitEvent();
        }

        private void InitMember()
        {
            this.NotificationFeeds = new RangeCollection<FeedItemViewModel>();
            this.UpdateFeeds = new RangeCollection<FeedItemViewModel>();
            ResetFeedCollectionRange();
        }

        private void ResetFeedCollectionRange()
        {
            this.NotificationFeeds.ResetMaxCount(ClientConfigurationManagement.Instance.GetSetting<int>("NotificationDisplayCount"));
            this.UpdateFeeds.ResetMaxCount(ClientConfigurationManagement.Instance.GetSetting<int>("UpdateDisplayCount"));
        }

        private void InitEvent()
        {
            this.InitGlobalEvent();
            this.InitLocalEvent();
        }

        private void InitGlobalEvent()
        {
            FeedUpdateEvent.Instance.Subscribe(this.OnFeedUpdate, ThreadOption.UIThread);
        }
        private void InitLocalEvent()
        {

        }


        private void OnFeedUpdate(FeedUpdateArgs args)
        {
            args.NotificationCollection.Reverse().Foreach(n => this.NotificationFeeds.InsertItemAtFirst(this.GenerateViewModel(n)));
            args.UpdateCollection.Reverse().Foreach(u => this.UpdateFeeds.InsertItemAtFirst(this.GenerateViewModel(u)));
        }

        private FeedItemViewModel GenerateViewModel(Feed model)
        {
            return new FeedItemViewModel(model);
        }
    }
}